﻿using MCPlatform.MotionLib.Base;
using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using MCPlatform.MotionLib.Base.Models;
using Newtonsoft.Json;
using Panuon.WPF;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MCPlatform.MotionLib.Base.Models
{
    public class ValveParaModel : NotifyPropertyChangedBase, INameProvider
    {
        [JsonIgnore]
        public IValve? ValveEntity { get => _ValveEntity; set => Set(ref _ValveEntity, value); }

        [JsonIgnore]
        private IValve? _ValveEntity;
        public string ControlName
        {
            get => _ControlName;
            set
            {
                Set(ref _ControlName, value);

            }
        }
        private string _ControlName = string.Empty;


        public int BasingTimeOut
        {
            get => _BasingTimeOut;
            set => Set(ref _BasingTimeOut, value);
        }
        private int _BasingTimeOut = 10000;


        public int WorkingTimeOut
        {
            get => _WorkingTimeOut;
            set => Set(ref _WorkingTimeOut, value);
        }
        private int _WorkingTimeOut = 10000;



        [JsonIgnore]
        public ICommand? ApplyCommand
        {
            get => _ApplyCommand;
            set => Set(ref _ApplyCommand, value);
        }
        [JsonIgnore]
        private ICommand? _ApplyCommand;

        [JsonIgnore]
        public ICommand? ApplyAndSaveCommand
        {
            get => _ApplyAndSaveCommand;
            set => Set(ref _ApplyAndSaveCommand, value);
        }
        [JsonIgnore]
        private ICommand? _ApplyAndSaveCommand;


        public ValveParaModel()
        {
            ApplyCommand = new Command(Apply);

            ApplyAndSaveCommand = new Command(ApplyAndSave);

            ControlName = ValveEntity is null ? string.Empty : ValveEntity.ValveName;
        }



        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="O"></param>
        public void Apply(Object O)
        {
            if (ValveEntity is null)
            {
                return;
            }

            ValveEntity.BaseTimeout = BasingTimeOut;

            ValveEntity.WorkTimeout = WorkingTimeOut;

            Toast.Show("应用成功!", targetWindow: ToastWindow.MainWindow);
        }

        /// <summary>
        /// 应用并保存
        /// </summary>
        /// <param name="O"></param>
        public void ApplyAndSave(Object O)
        {
            if (ValveEntity is null)
            {
                return;
            }

            ControlName = ValveEntity.ValveName;

            ValveEntity.BaseTimeout = BasingTimeOut;

            ValveEntity.WorkTimeout = WorkingTimeOut;

            String path = Paths.ValveConfigPaths;

            SaveConfigData.SaveConfig<ValveParaModel>(path, this);

        }

        public void MappingFromEntity()
        {
            if (ValveEntity is null)
            {
                return;
            }
            ValveEntity.ValveConfig.ControlName = ValveEntity.ValveName;
            //  Common.AutoMapPropertiesNotInterface(MotionEntity, this);
          
            Common.AutoMapPropertiesNotInterface(ValveEntity.ValveConfig, this);
        }

    }
}
